package org.community.detail.service.detail.userInfo;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.community.detail.repository.dao.User;
import org.community.detail.service.common.converter.EntityConverter;
import org.community.detail.service.common.converter.JSONConverter;
import org.community.detail.service.common.update.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;

import java.util.Map;

@Service
@Data
@Slf4j
@Transactional(
        isolation = Isolation.REPEATABLE_READ
)
public class UserInfoServiceMain {

    @Resource
    private JSONConverter jsonConverter;

    @Resource
    private EntityConverter entityConverter;

    @Resource
    private Update redisUpdate;

    @Resource
    private DataBaseService dataBaseService;




    //用户信息更新
    public int userInfoUpdate(JSONObject userInfoJson) throws ParseException, IOException {
        Map<String,Object> dataMap = this.getJsonConverter().JSONToMap(userInfoJson,"userInfo");


        String username = dataMap.get("username").toString();
        Integer userId = Integer.valueOf(dataMap.get("id").toString());
        //用户名是否与原来相同
        if(!this.getDataBaseService().isUsernameUpdated(username,userId)) {
            //如果用户名已存在
            if (this.getDataBaseService().isUsernameExist(dataMap.get("username").toString())) {
                return -1;
            }
        }
        Map<String, User> userMap = this.getEntityConverter().mapToUser(
                dataMap
        );

        //存入数据库
        String updateUserName = this.getDataBaseService().updateUserInfoInDB(userMap);
        if( updateUserName != null){
            User user = this.getDataBaseService().getUserInfoFromDB(userId).get(updateUserName);
            Map<String,Object> infoMap = this.getEntityConverter().userToMap(updateUserName,user);
            //更新认证redis
            redisUpdate.update(userId,infoMap);
            //更新业务缓存redis
            redisUpdate.updateToServiceDB(userId,infoMap);
            return 1;
        }

        return 0;


    }

    //用户信息初始化显示
    public Map<String,Object> userInfoShow(Integer userId,boolean uploadToRedis) throws ParseException, IOException {
        log.info("userInfoShow开始执行");
        log.info("id:{}",userId);
        if(this.getDataBaseService().isIdExist(userId)){
            //从数据库获取信息
            Map<String,User> userMap = this.getDataBaseService().getUserInfoFromDB(userId);
            log.info("信息：{}",userMap);
            Map<String,Object> infoMap = this.getEntityConverter().userMapToMap(userMap);
//            log.info("信息：{}",infoMap);
            if(uploadToRedis){
                //将信息上传至redis业务缓存库
                redisUpdate.uploadToServiceDB(userId,infoMap);
            }

            return infoMap;
        }
       return null;

    }

}
